using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Departure
{
    public partial class ctrlCountdown : UserControl
    {

        public delegate void CountdownFinishedEventHandler();
        public event CountdownFinishedEventHandler OnCountdownFinished;


        TimeSpan countDown;
        DateTime alarmDate;
        string alarmText = string.Empty;
        string trainText = string.Empty;
        string laterText = string.Empty;

        public ctrlCountdown()
        {
            InitializeComponent();
        }

        public void Start(DateTime alarmTime, string train, string later)
        {
            this.alarmDate = alarmTime;
            this.trainText = train;
            this.laterText = later;

            tmrCountdown.Enabled = true;
        }

        public void Stop()
        {
            tmrCountdown.Enabled = false;
        }

        public bool isRunning
        {
            get
            {
             return tmrCountdown.Enabled;
            }
        }

        private void CreateAndDraw()
        {
            // Upper Train Information
            Bitmap bmpTrain = new Bitmap(this.Size.Width, 16);
            Graphics gTrain = Graphics.FromImage(bmpTrain);

            gTrain.Clear(Color.White);

            try
            {
                using (Font f = new Font("Tahoma", 9, FontStyle.Regular))
                {
                    SizeF size;
                    using (Font f2 = AppropriateFont(gTrain, 5, 200, new Size(this.Size.Width, 16), trainText, f, out size))
                    {
                        gTrain.DrawString(trainText, f2, new SolidBrush(Color.Black), (this.Size.Width - size.Width) / 2, (16 - size.Height) / 2);
                    }
                }
            }
            catch (Exception ex)
            {
            }

            // Upper Current Time Information
            Bitmap bmpCurrent = new Bitmap(this.Size.Width, 16);
            Graphics gCurrent = Graphics.FromImage(bmpCurrent);

            gCurrent.Clear(Color.White);

            string currentText = DateTime.Now.ToString();

            try
            {
                using (Font f = new Font("Tahoma", 9, FontStyle.Regular))
                {
                    SizeF size;
                    using (Font f2 = AppropriateFont(gCurrent, 5, 200, new Size(this.Size.Width, 16), currentText, f, out size))
                    {
                        gCurrent.DrawString(currentText, f2, new SolidBrush(Color.Black), (this.Size.Width - size.Width) / 2, (16 - size.Height) / 2);
                    }
                }
            }
            catch (Exception ex)
            {
            }

            // Middle Time Countdown
            Bitmap bmpCountdown = new Bitmap(this.Size.Width, this.Size.Height - 48);
            Graphics gCountdown = Graphics.FromImage(bmpCountdown);

            // Clear the bitmap background.
            gCountdown.Clear(Color.White);

            try
            {
                using (Font f = new Font("Tahoma", 15, FontStyle.Regular))
                {
                    SizeF size;
                    using (Font f2 = AppropriateFont(gCountdown, 5, 200, new Size(this.Size.Width, this.Size.Height - 48), alarmText, f, out size))
                    {
                        if (laterText == "")
                            gCountdown.DrawString(alarmText, f2, new SolidBrush(Color.Black), (this.Size.Width - size.Width) / 2, (this.Size.Height - 48 - size.Height) / 2);
                        else
                            gCountdown.DrawString(alarmText, f2, new SolidBrush(Color.Red), (this.Size.Width - size.Width) / 2, (this.Size.Height - 48 - size.Height) / 2);
                    }
                }
            }
            catch (Exception ex)
            {
            }

            
            // Lower Later Information
            Bitmap bmpLater = new Bitmap(this.Size.Width, 16);
            Graphics gLater = Graphics.FromImage(bmpLater);

            gLater.Clear(Color.White);

            if (laterText != "")
            {
                try
                {
                    using (Font f = new Font("Tahoma", 9, FontStyle.Regular))
                    {
                        SizeF size;
                        using (Font f2 = AppropriateFont(gLater, 5, 200, new Size(this.Size.Width, 16), laterText, f, out size))
                        {
                            gLater.DrawString(laterText, f2, new SolidBrush(Color.Red), (this.Size.Width - size.Width) / 2, (16 - size.Height) / 2);
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }

            Bitmap bmpAll = new Bitmap(this.Size.Width, this.Size.Height);
            Graphics gAll = Graphics.FromImage(bmpAll);

            gAll.Clear(Color.White);

            gAll.DrawImage(bmpTrain, 0, 0);
            gAll.DrawImage(bmpCurrent, 0, 16);
            gAll.DrawImage(bmpCountdown, 0, 32);
            gAll.DrawImage(bmpLater, 0, this.Size.Height - 22);

            // Get a Graphics object for the form.
            Graphics g = CreateGraphics();

            g.DrawImage(bmpAll, 0, 0);

            // Clean up when we are done.
            g.Dispose();
            gCountdown.Dispose();
            bmpCountdown.Dispose();

            gTrain.Dispose();
            bmpTrain.Dispose();

            gCurrent.Dispose();
            bmpCurrent.Dispose();

            gLater.Dispose();
            bmpLater.Dispose();

            gAll.Dispose();
            bmpAll.Dispose();
        }


        public static Font AppropriateFont(Graphics g, float minFontSize, float maxFontSize, Size layoutSize, string s, Font f, out SizeF extent)
        {
            if (maxFontSize == minFontSize)
                f = new Font(f.Name, minFontSize, f.Style);

            extent = g.MeasureString(s, f);

            if (maxFontSize <= minFontSize)
                return f;

            float hRatio = layoutSize.Height / extent.Height;
            float wRatio = layoutSize.Width / extent.Width;
            float ratio = (hRatio < wRatio) ? hRatio : wRatio;

            float newSize = f.Size * ratio;

            if (newSize < minFontSize)
                newSize = minFontSize;
            else if (newSize > maxFontSize)
                newSize = maxFontSize;

            f = new Font(f.Name, newSize, f.Style);
            extent = g.MeasureString(s, f);

            return f;
        }

        private void tmrCountdown_Tick(object sender, EventArgs e)
        {
            try
            {
                countDown = alarmDate - DateTime.Now;
                alarmText = countDown.Hours.ToString("00") + ":" + countDown.Minutes.ToString("00") + ":" + countDown.Seconds.ToString("00");

                if (countDown.TotalSeconds < 1)
                {
                    tmrCountdown.Enabled = false;
                    alarmText = "00:00:00";

                    if (OnCountdownFinished != null)
                        OnCountdownFinished();
                }

                CreateAndDraw();

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        private void ctrlCountdown_Paint(object sender, PaintEventArgs e)
        {
            if (!isRunning)
                CreateAndDraw();
        }
    }
}
